package com.meida.module.bus.provider.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusReportOuttwo;
import com.meida.module.bus.provider.mapper.BusReportOuttwoMapper;
import com.meida.module.bus.provider.service.BusReportOuttwoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 接口实现类
 *
 * @author flyme
 * @date 2020-11-21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusReportOuttwoServiceImpl extends BaseServiceImpl<BusReportOuttwoMapper, BusReportOuttwo> implements BusReportOuttwoService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReportOuttwo bro, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReportOuttwo> cq, BusReportOuttwo bro, EntityMap requestMap) {
        cq.like("bro.batchNumber", requestMap.get("batchNumber"));
        cq.like("bro.reportMonth", requestMap.get("reportMonth"));
        cq.eq("bro.projectCode", requestMap.get("projectCode"));
        cq.orderByDesc("bro.reportMonth,bro.batchNumber");
        cq.createJoin(BusProject.class).setMainField("projectCode").setJoinField("projectCode");
        return ResultBody.ok();
    }

    @Override
    public ResultBody importBusReportOutTwoData(MultipartFile file, Map params) {
        try {
            String reportMonth = (String) params.get("reportMonth");
            Integer batchNumber = TypeUtils.castToInt(params.get("batchNumber"));
            String projectCode = (String) params.get("projectCode");
            //大坑 ------------导入时遇到单元格合并 列名相同时，需在实体类加fixedIndex进行区分
            ImportParams importParams = new ImportParams();
            importParams.setHeadRows(2);
            List<BusReportOuttwo> busReportOuttwoList = ExcelImportUtil.importExcel(file.getInputStream(), BusReportOuttwo.class, importParams);
            for (BusReportOuttwo busReportOuttwo : busReportOuttwoList) {
                busReportOuttwo.setReportOutTwoId(IdWorker.getId());
                busReportOuttwo.setBatchNumber(batchNumber);
                busReportOuttwo.setReportMonth(reportMonth);
                busReportOuttwo.setProjectCode(projectCode);
            }
            if (busReportOuttwoList.size() > 0) {
                saveBatch(busReportOuttwoList);
                return ResultBody.failed("导入成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.failed("导入失败");
    }

    @Override
    public List<EntityMap> getExportData(Map params) {
        CriteriaQuery<BusReportOuttwo> cq = new CriteriaQuery(BusReportOuttwo.class);
        cq.eq("projectCode", params.get("projectCode"));
        cq.eq("reportMonth", params.get("reportMonth"));
        cq.eq("batchNumber", params.get("batchNumber"));
        return selectEntityMap(cq);
    }
}
